import router from '../router'
const mutations = {
  setActiveMenu(state, data){//设置当前选中的导航栏菜单
    state.activeMenu = data
  },
  setMenuArray(state, data){//设置历史选中的导航栏菜单
    let old = state.menuArray
    let result = old.some(item=>{
      if(item.index === data.index) {
        return true
      }
    })
    if(!result) {
      old.push(data)
      state.menuArray = old
    }
  },
  delMenuArray(state, data){//删除历史选中的导航栏菜单
    let old = state.menuArray
    if(state.activeMenu.index === data.index) {
      for(let idx in old) {
        if(old[idx].index === data.index) {
          old.splice(idx, 1)
          state.activeMenu = old[idx -1]
          router.push({path: old[idx -1].index})
        }
      }
    }else{
      for(let idx in old) {
        if(old[idx].index === data.index) {
          old.splice(idx, 1)
        }
      }
    }
    
    state.menuArray = old
  },
  clearMenuArray(state, data) {
    state.menuArray = data
  },
  addKeepAlive(state, data) {
    if( state.keepAlive.indexOf(data) == -1 ) {
      state.keepAlive.push(data)
    }
  },
  dleKeepAlive(state, data) {
    for(let idx in state.keepAlive) {
      if(state.keepAlive[idx] === data) {
        state.keepAlive.splice(idx, 1)
      }
    }
  },
  clearKeepAlive(state) {
    state.keepAlive = []
  },
  setLang(state, data) {
    state.lang = data
  },
  setToken(state, data) {
    state.token = data
  },
  setCode(state, data) {
    state.ocode = data
  },
  setMenu(state, data) {
    state.menu = data
  },
  setPermissions(state, data) {
    state.permissions = data
  },
  setEquipmentModel(state, data) {
    state.equipmentModel = data
  },
  setMemusid(state, data) {
    state.memusid = data
  },
  setUid(state, data) {
    state.uid = data
  },
  setName(state, data) {
    state.name = data
  }
}

export default mutations